﻿using System.Collections.Generic;
using System.Text;
using HCQ2_IDAL;
using HCQ2_Model.Log;
using HCQ2_Common;
using HCQ2_Common.SQL;

namespace HCQ2_DAL_MSSQL
{
    public partial class T_VROperatorLogDAL: IT_VROperatorLogDAL
    {
        /// <summary>
        ///  获取日志列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<ResultLogModel> GetLogTableList(QueryModel param,out int total)
        {
            sb?.Clear();
            string tempSql = GetLogQuerySql(param);
            sb.Append(
                $@"SELECT TOP {param.rows} * FROM (SELECT ROW_NUMBER() OVER(PARTITION BY module_code,page_name ORDER BY create_date ASC) AS number,* FROM (SELECT log_code,ip,user_id,user_name,module_name,module_code,page_name,button_name,context,CONVERT(varchar(100),create_date,20)AS create_date FROM T_VROperatorLog WHERE 1=1 {tempSql};");
            sb.Append($")ls)logs WHERE logs.number>{(param.page - 1) * param.rows}");
            string tempTotal = $" SELECT COUNT(0) AS num FROM T_VROperatorLog WHERE 1=1 {tempSql};";
            total= Helper.ToInt(SqlHelper.ExecuteScalar(tempTotal));
            return SqlHelper.ExecuteList<ResultLogModel>(sb.ToString());
        }
        private string GetLogQuerySql(QueryModel param)
        {
            StringBuilder d = new StringBuilder();
            //操作用户
            if (!string.IsNullOrEmpty(param.user_name))
                d.Append($" AND user_name LIKE '%{param.user_name}%' ");
            //模块代码
            if (!string.IsNullOrEmpty(param.module_code))
                d.Append($" AND module_code='{param.module_code}' ");
            //页面名称
            if (!string.IsNullOrEmpty(param.page_name))
                d.Append($" AND page_name='{param.page_name}' ");
            //操作开始日期
            if (!string.IsNullOrEmpty(param.in_date) && !string.IsNullOrEmpty(param.end_date))
                d.Append($" AND create_date>= '{param.in_date}' AND create_date<='{param.end_date}' ");
            else if (!string.IsNullOrEmpty(param.in_date) && string.IsNullOrEmpty(param.end_date))
                d.Append($" AND create_date>= '{param.in_date}' ");
            else if (string.IsNullOrEmpty(param.in_date) && !string.IsNullOrEmpty(param.end_date))
                sb.AppendFormat($" AND create_date<= '{param.end_date}' ");
            return d.ToString();
        }
    }
}
